// 数据统计工具
const DataStats = {
    // 获取课程总数
    getTotalCourses() {
        return coursesData.length;
    },

    // 按分类统计课程数量
    getCoursesByCategory() {
        const stats = {};
        categoriesData.forEach(category => {
            const count = coursesData.filter(course => course.category_id === category.id).length;
            stats[category.name] = count;
        });
        return stats;
    },

    // 获取推荐课程数量
    getRecommendedCount() {
        return coursesData.filter(course => course.recommend).length;
    },

    // 获取注册人数最多的课程
    getMostPopularCourse() {
        return coursesData.reduce((max, course) => 
            course.registerCount > max.registerCount ? course : max
        );
    },

    // 获取最新的课程
    getLatestCourse() {
        return coursesData.reduce((latest, course) => 
            new Date(course.created_at) > new Date(latest.created_at) ? course : latest
        );
    },

    // 打印完整统计信息
    printStats() {
        console.log('===Course data statistics===');
        console.log(`Total courses: ${this. getTotalCourses()}`);
        console.log(`Recommended courses: ${this. getRecommendedCount()}`);
        
        console.log('\Classification statistics:');
        const categoryStats = this.getCoursesByCategory();
        Object.entries(categoryStats).forEach(([category, count]) => {
            console.log(`${category}: ${count} courses`);
        });

        const mostPopular = this.getMostPopularCourse();
        console.log(`\Most popular course: ${mostPopular. title} (${mostPopular. registrant Count} people registered)`);

        const latest = this.getLatestCourse();
        console.log(`Latest Course: ${latex. title} (${latex. creatd_at})`);

        console.log('\Preview of Course List:');
        coursesData.slice(0, 5).forEach(course => {
            console.log(`-${course. title} | ${course. curriculum} | ${course. tutor} | ${course. registrant Count} person`);
        });
    },

    // 验证数据完整性
    validateData() {
        const errors = [];
        
        // 检查必填字段
        coursesData.forEach((course, index) => {
            if (!course.title) errors.push(`Course ${index+1} lacks title`);
            if (!course.teacher_id) errors.push(`Course ${index+1} is missing teacher ID`);
            if (!course.category_id) errors.push(`Course ${index+1} is missing category ID`);
            if (!course.status) errors.push(`Course ${index+1} is missing status`);
        });

        // 检查ID唯一性
        const ids = coursesData.map(course => course.id);
        const uniqueIds = [...new Set(ids)];
        if (ids.length !== uniqueIds.length) {
            errors.push('There are duplicate course IDs present');
        }

        if (errors.length === 0) {
            console.log('✅  Data validation passed');
        } else {
            console.log('❌  Data validation failed:');
            errors.forEach(error => console.log(`  - ${error}`));
        }

        return errors.length === 0;
    }
};

// 自动执行统计（可选）
if (typeof coursesData !== 'undefined') {
    // 延迟执行，确保数据已加载
    setTimeout(() => {
        DataStats.printStats();
        DataStats.validateData();
    }, 100);
}

// 导出到全局
window.DataStats = DataStats; 